package com.itheima.edu.info.manager.entry;

import com.itheima.edu.info.manager.controller.OtherStudentController;
import com.itheima.edu.info.manager.controller.OtherTeacherController;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;

/*
程序的入口类
 */
@SuppressWarnings("all")
public class ClientEntry{
    public static Scanner sc = new Scanner(System.in);
    public static Properties prop = new Properties();

    public static void main(String[] args) {

        while (true) {
            System.out.println("-----1.登录-----2.退出-----");
            String str1 = sc.next();
            switch (str1) {
                case "1":
                    login();
                    break;
                case "2":
                    return;
                default:
                    System.out.println("输入有误!请重新输入:");
                    break;
            }
        }
    }

    private static void login() {
        String str = new String();
        for (char da = 'A', xiao = 'a'; da <= 'Z' && xiao <= 'z'; da++, xiao++) {
            str += da;
            str += xiao;
        }
        str += "0123456789";
        try {
            InputStreamReader fr = new InputStreamReader(new FileInputStream("StudentClient\\long.properties"), "gbk");
            prop.load(fr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        OtherStudentController studentController = new OtherStudentController();
        OtherTeacherController teacherController = new OtherTeacherController();
        while (true) {
            String s = "";
            for (int i = 0; i < 4; i++) {
                Random r = new Random();
                int i1 = r.nextInt(str.length());
                char c = str.charAt(i1);
                s += c;
            }

            System.out.println("请输入用户名:");
            String username = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            System.out.println("生成的验证码为：" + s);
            System.out.println("请输入验证码：");
            String code = sc.next();
            if (username.equals(prop.getProperty("username")) && password.equals(prop.getProperty("password")) && s.equalsIgnoreCase(code)) {
                while (true) {
                    System.out.println("--------欢迎来到黑马信息管理系统--------");
                    System.out.println("请输入您的选择: 1.学生管理  2.老师管理  3.退出");
                    String choice = sc.next();
                    switch (choice) {
                        case "1":
                            studentController.studentStart();
                            break;
                        case "2":
                            teacherController.teacherStart();
                            break;
                        case "3":
                            System.out.println("感谢您的使用!");
                            System.exit(0);
                            break;
                        default:
                            System.out.println("您的输入有误,请重新输入:");
                            break;
                    }
                }
            } else if (!s.equalsIgnoreCase(code)) {
                System.out.println("验证码错误!请重新输入:");
            } else {
                System.out.println("你输入的用户名或密码错误,请重新输入：");
            }
        }
    }
}
